//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.rjxy.demo.controller;

import com.rjxy.demo.common.Result;
import com.rjxy.demo.pojo.HeroInfo;
import com.rjxy.demo.server.HeroInfoServer;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/hero")
public class HeroInfoController {
    @Autowired
    private HeroInfoServer heroInfoServer;
    private static final Logger log = Logger.getLogger(HeroInfoController.class);

    public HeroInfoController() {
    }

    @GetMapping("/getJsonAll")
    public Result<List<HeroInfo>> getJsonAll() {
        try {
            List<HeroInfo> list = heroInfoServer.getAll();
            return Result.success(list);
        } catch (Exception e) {
            log.error("查询英雄列表失败", e);
            return Result.error("查询失败");
        }
    }

    @GetMapping("/{hid}")
    public Result<HeroInfo> getById(@PathVariable Integer hid) {
        try {
            HeroInfo heroInfo = heroInfoServer.getByHid(hid);
            return Result.success(heroInfo);
        } catch (Exception e) {
            log.error("查询英雄详情失败", e);
            return Result.error("查询失败");
        }
    }

    @PostMapping("/addHero")
    public Result<Void> addHero(@RequestBody HeroInfo heroInfo) {
        try {
            log.info("进入新增英雄功能，参数：" + heroInfo);
            heroInfoServer.saveHero(heroInfo);
            return Result.success(null);
        } catch (Exception e) {
            log.error("新增英雄失败", e);
            return Result.error("新增失败");
        }
    }

    @DeleteMapping("/removeHero")
    public Result<Void> removeHero(Integer hid) {
        try {
            log.info("进入删除英雄功能，参数：" + hid);
            heroInfoServer.removeHero(hid);
            return Result.success(null);
        } catch (Exception e) {
            log.error("删除英雄失败", e);
            return Result.error("删除失败");
        }
    }

    @RequestMapping({"showDate"})
    public String showDate(Integer hid, Model model) {
        log.info("进入修改功能的数据数据回显请求，参数：" + hid);
        HeroInfo heroInfo = this.heroInfoServer.getByHid(hid);
        model.addAttribute("hero", heroInfo);
        log.info("返回结果：" + heroInfo);
        return "hero/updateHero";
    }

    @PostMapping("/updateHero")
    public Result<Void> updateHero(@RequestBody HeroInfo heroInfo) {
        try {
            log.info("进入修改功能，参数：" + heroInfo);
            heroInfoServer.updateHero(heroInfo);
            return Result.success(null);
        } catch (Exception e) {
            log.error("修改英雄失败", e);
            return Result.error("修改失败");
        }
    }
}
